﻿define([
// Dojo
        "dojo/_base/connect",
        "dojo/_base/lang",
        "dojo/_base/declare",
        "dojo/html",
        "dojo/dom-construct",
        "dojo/dom-style",
        "dojo/dom-attr",
// Dijit
        "dijit/_TemplatedMixin",
        "dijit/_WidgetBase",
        "dijit/_WidgetsInTemplateMixin",
        "epi/shell/command/_WidgetCommandProviderMixin",
        "dojo/dnd/Source",
//CMS
        "epi/cms/_ContentContextMixin",
        "epi/dnd/Target",
        "epi/shell/command/_Command",
        "epi/shell/widget/dialog/Dialog",

        "epi/shell/command/_CommandProviderMixin",

        "epi/cms/legacy/LegacyDialogPopup",
        "epi/cms/widget/_HasChildDialogMixin",

        "ow-epi-videocloud/knockout-2.2.1",
        "ow-epi-videocloud/jquery.nanoscroller",
        "ow-epi-videocloud/BrowserViewModel",
        "dojo/text!./browserWidgetTemplate.htm",
        "xstyle/css!./nanoscroller.css",
        "xstyle/css!./BrowserWidget.css"
    ], function (
    // Dojo
        connect,
        lang,
        declare,
        html,
        domConstruct,
        domStyle,
        domAttr,
    // Dijit
        _TemplatedMixin,
        _WidgetBase,
        _WidgetsInTemplateMixin,
        _WidgetCommandProviderMixin,
        DndSource,
    //CMS
        _ContentContextMixin,
        DndTarget,
        _Command,
        Dialog,

        _CommandProviderMixin, //used as constructor not as mixin

        LegacyDialogPopup,
        _HasChildDialogMixin,

        ko,
        nanoscroller,
        BrowserViewModel,
        template
    ) {
        return declare("ow-epi-videocloud.BrowserWidget",
            [_WidgetBase, _TemplatedMixin, _ContentContextMixin, _WidgetsInTemplateMixin, _WidgetCommandProviderMixin, _HasChildDialogMixin], {
                postCreate: function () {
                    // call base implementation
                    this.inherited(arguments);

                    if (!this.fileControllerUrl) {
                        alert('fileControllerUrl not defined');
                        return;
                    }
                    
                    this.viewModel = new BrowserViewModel(this.fileControllerUrl, this.selectedVideoId, this.players);
                    this.viewModel.browserWidget = this;
                    
                    ko.applyBindings(this.viewModel, this.browserContainer);
                },

                templateString: template,

                _dialog: null,
                
                selectedItem: '',

                _onDialogCallback: function (value) {
                    return;
                },

                _onDialogHide: function () {
                    this.isShowingChildDialog = false;

                    this._dialog.destroyRecursive();
                    delete this._dialog;
                }

            });
    });